pub mod batching; // 导出动态批处理

pub struct AdaptiveBatcher {
    max_batch_size: usize,
    current_batch: usize,
    latency_history: VecDeque<Duration>,
}

impl AdaptiveBatcher {
    pub fn new(initial_batch: usize) -> Self {
        Self {
            max_batch_size: initial_batch,
            current_batch: initial_batch,
            latency_history: VecDeque::with_capacity(10),
        }
    }

    pub fn adjust_batch_size(&mut self, latency: Duration) {
        self.latency_history.push_back(latency);
        if self.latency_history.len() > 10 {
            self.latency_history.pop_front();
        }

        let avg_latency = self.latency_history.iter().sum::<Duration>() / self.latency_history.len() as u32;
        if avg_latency < Duration::from_millis(100) {
            self.current_batch = (self.current_batch * 2).min(self.max_batch_size);
        } else {
            self.current_batch = (self.current_batch / 2).max(1);
        }
    }
}

// 零拷贝数据管道
pub struct ZeroCopyPipe {
    #[cfg(unix)]
    mem: memmap2::MmapMut,
}

impl ZeroCopyPipe {
    pub fn new(size: usize) -> Result<Self, std::io::Error> {
        #[cfg(unix)] {
            let file = tempfile::tempfile()?;
            file.set_len(size as u64)?;
            let mem = unsafe { memmap2::MmapMut::map_mut(&file)? };
            Ok(Self { mem })
        }
        #[cfg(not(unix))] {
            // 其他平台实现
            Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                "Not supported on this platform",
            ))
        }
    }
}